﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FlickrSample.SL.Infrastructure;
using FlickrSample.SL.Infrastructure.Flickr.Entities;
using XamlingCore.Infrastructure.View;

namespace FlickrSample.View.Photo
{
    public class PhotoSetViewModel : XContentViewModelBase
    {
        private PhotoSet photoSet;

        private FlickrPhotoInstance defaultInstance;

        private ObservableCollection<PhotoItem> instances = new ObservableCollection<PhotoItem>();

        private PhotoItem selectedItem;

        public class PhotoItem
        {
            public FlickrPhoto Photo { get; set; }
            public FlickrPhotoInstance Instance { get; set; }
        }

        public override void OnInitialise()
        {
            Title = photoSet.Title;
            if (photoSet.Photos == null || photoSet.Photos.Count == 0)
            {
                IsLoading = true;
                ServiceLocator.PhotoRepo.GetPhotos(photoSet, (result) =>
                                                             {
                                                                 photoSet.Photos = result;
                                                                 if (photoSet.Photos.Count > 0)
                                                                 {
                                                                     ServiceLocator.PhotoRepo.GetSizes(photoSet.Photos[0], () =>
                                                                                                                               {
                                                                                                                                   if (photoSet.Photos[0].PhotoInstances.Count > 3)
                                                                                                                                   {
                                                                                                                                       DefaultInstance
                                                                                                                                           =
                                                                                                                                           photoSet
                                                                                                                                               .
                                                                                                                                               Photos
                                                                                                                                               [
                                                                                                                                                   0
                                                                                                                                               ]
                                                                                                                                               .
                                                                                                                                               PhotoInstances
                                                                                                                                               [
                                                                                                                                                   3
                                                                                                                                               ];
                                                                                                                                   }
                                                                                                                                   if (photoSet.Photos[0].PhotoInstances.Count > 0)
                                                                                                                                   {
                                                                                                                                       instances.Add(new PhotoItem()
                                                                                                                                       {
                                                                                                                                           Instance = photoSet.Photos[0].PhotoInstances[0],
                                                                                                                                           Photo = photoSet.Photos[0]
                                                                                                                                       });
                                                                                                                                   }
                                                                                                                                   loadInstances
                                                                                                                                       ();
                                                                                                                                   IsLoading = false;
                                                                                                                               });
                                                                 }
                                                                 else
                                                                 {
                                                                     IsLoading = false;
                                                                 }

                                                             });
            }
        }

        private void onPhotoSelected()
        {
            var model = CreateContentModel<PhotoDetailViewModel>(newItem =>
                                                                     {
                                                                         newItem.Photo = selectedItem.Photo;
                                                                     });
            NavigateTo(model);
        }



        void loadInstances()
        {
            foreach (var itemIt in photoSet.Photos)
            {
                var item = itemIt;
                if (item.PhotoInstances == null || item.PhotoInstances.Count == 0)
                {
                    ServiceLocator.PhotoRepo.GetSizes(item, () =>
                                                                {
                                                                    if (item.PhotoInstances.Count > 0)
                                                                    {
                                                                        instances.Add(new PhotoItem()
                                                                                          {
                                                                                              Instance =
                                                                                                  item.PhotoInstances[0],
                                                                                              Photo = item
                                                                                          });

                                                                    }
                                                                });
                }
                else
                {
                    if (item.PhotoInstances.Count > 0)
                    {
                        var existing = instances.Where(p => p.Photo == item).FirstOrDefault();

                        if (existing == null)
                        {
                            instances.Add(new PhotoItem()
                                              {
                                                  Instance = item.PhotoInstances[0],
                                                  Photo = item
                                              });
                        }
                    }

                }

            }
        }



        public PhotoSet PhotoSet
        {
            get { return photoSet; }
            set
            {
                photoSet = value;
                OnPropertyChanged("PhotoSet");
            }
        }

        public FlickrPhotoInstance DefaultInstance
        {
            get { return defaultInstance; }
            set
            {
                defaultInstance = value;
                OnPropertyChanged("DefaultInstance");
            }
        }

        public ObservableCollection<PhotoItem> Instances
        {
            get { return instances; }
            set
            {
                instances = value;
                OnPropertyChanged("Instances");
            }
        }

        public PhotoItem SelectedItem
        {
            get { return selectedItem; }
            set
            {
                selectedItem = value;
                OnPropertyChanged("SelectedItem");
                onPhotoSelected();
            }
        }

       
    }
}
